<?php
	$dirFromZip = '';

    define('DIRECTORY_DELIMITER', '\\');

    $zipDir = getcwd() . DIRECTORY_DELIMITER;

    $zip = zip_open($EXTFILE);
	$vs_size = filesize($EXTFILE);

    if (is_resource($zip))
    { 
        while ($zip_entry = zip_read($zip))
        {
            $completePath =  dirname(zip_entry_name($zip_entry));
            $completeName =  zip_entry_name($zip_entry);
        
            // Walk through path to create non existing directories
            // This won't apply to empty directories ! They are created further below
          if(!file_exists($completePath) && preg_match( '#^' . $dirFromZip .'.*#', dirname(zip_entry_name($zip_entry)) ) )
            {
                $tmp = '';
                foreach(explode('/',$completePath) AS $k)
                {
                    $tmp .= $k.'/';
                    if(!file_exists($tmp) )
                    {
                        @mkdir($tmp, 0777);
                    }
                }
            }
           
            if (zip_entry_open($zip, $zip_entry, "r"))
            {
                if( preg_match( '#^' . $dirFromZip .'.*#', dirname(zip_entry_name($zip_entry)) ) )
                {
				if ($completeName == 'ALT.CSV' || $completeName == 'SDN.CSV' || $completeName == 'ADD.CSV') {
				
                    if ($fd = @fopen($completeName, 'w+'))
                    {
                        fwrite($fd, zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)));
                        fclose($fd);
                    }
                    else
                    {
                        // We think this was an empty directory
                        mkdir($completeName, 0777);
                    }
                    zip_entry_close($zip_entry);
                }
				}
            }
        }
        zip_close($zip);
    }

	$Line1 = '';
	$Line2 = '';
	$Line3 = '';
	// UPDATE
	$TmpFile = 'ADD.CSV';
	$row = 0;
	$handle = fopen($TmpFile, "r");
	$Rows = count(file($TmpFile))-1;
		   
			while (($data = fgetcsv($handle, $Rows, ",")) !== FALSE) {
				
				$row++;
				if ($row <= $Rows) {
				$Line1 .= '('.$data[0].','.$data[1].',"'.$data[2].'","'.$data[3].'","'.$data[4].'","'.$data[5].'",'.str_word_count($data[2]).'),';
				}
			}
		fclose($handle);
		unlink($TmpFile);
	
	$TmpFile = 'SDN.CSV';
	$row = 0;
	$handle = fopen($TmpFile, "r");
	$Rows = count(file($TmpFile))-1;
	
			while (($data = fgetcsv($handle, $Rows, ",")) !== FALSE) {
				$row++;
				if ($row <= $Rows) {
				$Line2 .= '('.$data[0].',"'.$data[1].'","'.$data[2].'","'.$data[3].'","'.$data[4].'","'.$data[5].'","'.$data[6].'","'.$data[7].'","'.$data[8].'","'.$data[9].'","'.$data[10].'","'.$data[11].'",'.str_word_count($data[1]).'),';
				}
			}
		
	fclose($handle);
	unlink($TmpFile);
	
	$TmpFile = 'ALT.CSV';
	$row = 0;
	$handle = fopen($TmpFile, "r");
	$Rows = count(file($TmpFile))-1;
	
			while (($data = fgetcsv($handle, $Rows, ",")) !== FALSE) {
				$row++;
				if ($row <= $Rows) {
				$Line3 .= '('.$data[0].','.$data[1].',"'.$data[2].'","'.$data[3].'","'.$data[4].'",'.str_word_count($data[3]).'),';
				}
			}
		fclose($handle);
		unlink($TmpFile);

	# DO PRC
		CONN('MYI');
		$GLOBALS['MYI']->multi_query("CALL  ext_ofactables('{$FILEID}','{$vs_size}','{$sysTime}');");
		 do {
			if ($result = $GLOBALS['MYI']->store_result()) {
				while ($row = $result->fetch_row()) {
				}
				$result->free();
			}
		} while ($GLOBALS['MYI']->next_result());
		$GLOBALS['MYI']->close();

	## END PRC
		CONN('MYT');
		$GLOBALS['MYT']->db_query("INSERT INTO ext_ofac_address 
		 (Ent_num, Add_num, Address, City, Country, Add_remarks, Address_Length)
		 VALUES ".substr($Line1,0,strlen($Line1)-1));
		$GLOBALS['MYT']->db_query("INSERT INTO ext_ofac_main
		 (ent_num, SDN_Name, SDN_Type, Program, Title, Call_Sign, Vess_type, Tonnage, GRT, Vess_flag, Vess_owner, remarks, SDN_Length)
		 VALUES ".substr($Line2,0,strlen($Line2)-1));
		$GLOBALS['MYT']->db_query("INSERT INTO ext_ofac_altidentity
		 (ent_num, alt_num, alt_type, alt_name, alt_remarks, alt_Length)
		 VALUES ".substr($Line3,0,strlen($Line3)-1));
		$GLOBALS['MYT']->db_query("INSERT INTO ext_ofac_compact (ent_num, SDN_Name, alt_Name, Address, SDN_Length, alt_Length, Address_Length)
		SELECT ext_ofac_main.ent_num, SDN_Name, alt_Name, Address, SDN_Length, alt_Length, Address_Length
			FROM ext_ofac_main
				LEFT OUTER JOIN ext_ofac_address ON ext_ofac_main.ent_num = ext_ofac_address.ent_num
				LEFT OUTER JOIN ext_ofac_altidentity ON ext_ofac_main.ent_num = ext_ofac_altidentity.ent_num");
	// ENDUPDATE 